Ext.Loader.setConfig({enabled: true});
Ext.Loader.setPath({'Ext.ux' : 'extjs4/ux'});
Ext.require([
             'Ext.grid.*',
             'Ext.data.*',
             'Ext.ux.grid.FiltersFeature',
             'Ext.toolbar.Paging',
             'Ext.ux.ajax.JsonSimlet',
             'Ext.ux.ajax.SimManager',
             'Ext.ux.grid.Printer'        
]);
Ext.define('TS.controller.RpARecChecks',{
	extend: 'Ext.app.Controller',
	models:['rp.RpCheckModel','rp.ContactModel'],
	stores:['rp.ARecCheckLogStore'],
	views:['rp.RpARecCheckList','rp.RpARecCheckEditor'],
	init:function() {
		this.control({
			'rparecchecklist triggerfield[name=customer]':{specialkey:this.onSearchCust,change:this.onClearCust},
			'rparecchecklist button[action=search]':{click:this.onSearch},
			'rparecchecklist button[action=advsearch]':{click:this.onAdvSearch},
			'rparecchecklist button[action=check]':{click:this.onCheckBill},
			'rparecchecklist button[action=unCheck]':{click:this.onUnCheckBill},
			'rparecchecklist button[action=printBill]':{click:this.onPrintBill},
			'rparecchecklist button[action=exportBill]':{click:this.onExportBill}
		});
	},
	onSearch:function(button) {
		var begDate = button.up('panel').down('datefield[name=begDate]').getRawValue();
		var endDate = button.up('panel').down('datefield[name=endDate]').getRawValue();
		var custId = button.up('panel').down('hiddenfield[name=custId]').getValue();
		var grid = button.up('form').up('panel').down('grid');
		var store = grid.getStore();
		store.on("beforeload",function(){
	        Ext.apply(grid.store.proxy.extraParams, {begDate:begDate,endDate:endDate,custId:custId});
	    });
		store.loadPage(1,{
			params:{
				begDate:begDate,
				endDate:endDate,
				custId:custId
			}
		});
	},
	onAdvSearch:function(button) {
		
	},
	onSearchCust:function(field,e) {
		var custIdField = field.up('panel').down('hiddenfield[name=custId]');
		if(e.getKey() == e.ENTER && field.value != '') {
		    	var view=Ext.create('TS.view.base.CustomerSearchDialog');
		    	view.setVal(field.value);
		    	view.on({
				    selected: function(records){
				    	if(records.length == 0) {
				    		Ext.MessageBox.alert('ThinkGsp','未检索到任何客户信息!');
				    	} else {
					    	custIdField.setValue(records[0].get('customerId'));
					    	field.setValue('[' + records[0].get('customerCode') + ']' + records[0].get('customerName'));
				    	}
				    },
				    scope: this
				});
		    	view.show();	
	    }
	},
	onClearCust:function(field,e) {
		var custIdField = field.up('panel').down('hiddenfield[name=custId]');
		if(field.value == '') {
			//var custIdField = field.up('panel').down('hiddenfield[name=custId]');
			custIdField.setValue(null);
		}
	},
	onPrintBill:function(button) {
		var grid = button.up('gridpanel');
		Ext.ux.grid.Printer.print(grid);
	},
	onExportBill:function(button) {
		
	},
	onCheckBill:function(button) {
		var form = Ext.create('TS.view.rp.RpARecCheckEditor',{closable : true});
		//var form = Ext.create('TS.view.rp.RpPayBillEditor',{closable : true});
		var main = Ext.getCmp("syscontent");
		var panel = main.add(form);
		main.setActiveTab(panel);
	},
	onUnCheckBill:function(button) {
		var searchButton = button.up('grid').up('panel').down('form').down('button[action=search]');
		var grid = button.up('grid');
		var models = grid.getSelectionModel().getSelection();
		if(!models || models.length == 0) {
			Ext.Msg.show({
				title:'警告',
				msg:'您没有选择需要反核销的单据!',
				buttons:Ext.Msg.OK,
				icon:Ext.Msg.WARNING
			});
			return false;
		}
		var record = models[0];
		var checkId = record.get('checkId');
		if(!checkId || checkId == 0) {
			Ext.Msg.show({
				title:'警告',
				msg:'核销号无效!',
				buttons:Ext.Msg.OK,
				icon:Ext.Msg.WARNING
			});
			return false;
		}
		//var msg = '您是否要反核销选择的单据?';
		//msg += (record.get('checkType') == 4 ? '\n反核销【应收款转销】类型后，需要手工删除对应的收款单与应收单!' : '');
		
		Ext.Msg.show({
			title:'询问',
			msg:'您是否要反核销选择的单据?',
			buttons:Ext.Msg.YESNO,
			icon:Ext.Msg.QUESTION,
			fn:function(buttonId) {
				if(buttonId == 'yes') {
					this.doUnCheckBill(searchButton,checkId);
				} 
			},
			scope:this
		});
	},
	doUnCheckBill:function(searchButton,checkId) {
		var me = this;
		Ext.Ajax.request({
			url:'rp/areccheck/checkForRevoke.do',
			params:{checkId:checkId},
			success:function(response) {
		    	var data=Ext.decode(response.responseText);
		    	if(data.success==true){
		    		Ext.MessageBox.show({
		    			title:'ThinkGsp',
		    			msg:data.msg,
		    			buttons:Ext.Msg.OK,
		    			icon:Ext.Msg.INFO,
		    			fn:function(buttonId) {
		    				searchButton.fireEvent('click',searchButton);
		    			},
		    			scope:this
		    		});
		    	} else {
		    		Ext.MessageBox.show({
		    			title:'错误',
		    			msg:data.msg,
		    			buttons:Ext.Msg.OK,
		    			icon:Ext.Msg.ERROR
		    		});			    		
		    	}
			}
		});
	}
});	